/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package acousticfield3d.gui.panels;

import acousticfield3d.gui.MainForm;
import acousticfield3d.math.Vector3f;
import acousticfield3d.scene.Entity;
import acousticfield3d.scene.MeshEntity;
import acousticfield3d.scene.Resources;
import acousticfield3d.scene.Scene;
import acousticfield3d.simulation.ControlPoint;
import acousticfield3d.simulation.Transducer;
import acousticfield3d.utils.Color;
import acousticfield3d.utils.Parse;
import java.util.ArrayList;
import java.util.List;

import acousticfield3d.math.Ray;

/**
 *
 * @author Asier
 */
public class ControlHapticObject extends javax.swing.JPanel {
    public MainForm mf;
    
    public ControlHapticObject(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cpEnableCheck = new javax.swing.JCheckBox();
        jLabel41 = new javax.swing.JLabel();
        cpSizeText = new javax.swing.JTextField();
        jLabel43 = new javax.swing.JLabel();
        cpColorText = new javax.swing.JTextField();
        cpAddButton = new javax.swing.JButton();
        cpDelButton = new javax.swing.JButton();
        placeBeadAtSliceCheck = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        numberText = new javax.swing.JTextField();
        allVisibleButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        onlyVisibleButton = new javax.swing.JButton();
        selectAllButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        cpEnableCheck.setText("enable");
        cpEnableCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpEnableCheckActionPerformed(evt);
            }
        });

        jLabel41.setText("size:");

        cpSizeText.setText("0.05");
        cpSizeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpSizeTextActionPerformed(evt);
            }
        });

        jLabel43.setText("Color:");

        cpColorText.setText("127,0,255,255");
        cpColorText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpColorTextActionPerformed(evt);
            }
        });

        cpAddButton.setText("Add");
        cpAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpAddButtonActionPerformed(evt);
            }
        });

        cpDelButton.setText("Del");
        cpDelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpDelButtonActionPerformed(evt);
            }
        });

        placeBeadAtSliceCheck.setText("PlaceBeadAtSlice");

        jLabel1.setText("Number:");

        numberText.setText("0");

        allVisibleButton.setText("All");
        allVisibleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allVisibleButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Visible:");

        onlyVisibleButton.setText("O");
        onlyVisibleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onlyVisibleButtonActionPerformed(evt);
            }
        });

        selectAllButton.setText("S A");
        selectAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllButtonActionPerformed(evt);
            }
        });

        jButton1.setText("TrapFollow");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("NoTrap");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cpEnableCheck)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel41)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cpSizeText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel43)
                        .addGap(7, 7, 7)
                        .addComponent(cpColorText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(numberText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(allVisibleButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(onlyVisibleButton))
                            .addComponent(placeBeadAtSliceCheck)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(selectAllButton)
                                .addGap(18, 18, 18)
                                .addComponent(cpAddButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cpDelButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cpEnableCheck)
                    .addComponent(jLabel41)
                    .addComponent(cpSizeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel43)
                    .addComponent(cpColorText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(numberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(allVisibleButton)
                    .addComponent(onlyVisibleButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectAllButton)
                    .addComponent(cpAddButton)
                    .addComponent(cpDelButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(placeBeadAtSliceCheck)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public MeshEntity createControlPoint(float x, float y, float z, int frame, int number, boolean isBead){
        MeshEntity me = new ControlPoint(Resources.MESH_BOX, null, Resources.SHADER_SOLID_SPEC);
        
        me.setTag(Entity.TAG_OBJ);
        me.setFrame( frame );
        
        me.setColor( Color.parse( getControlPointColor() ) );
        me.getTransform().getScale().set( getControlPointSize() );
        me.getTransform().getTranslation().set(x, y, z);
        if (number == -1){
            number = getNumber();
        }
        me.setNumber( number );
        return me;
    }
    
    public MeshEntity addHapticObject(final Vector3f pos, int frame, int number, boolean isBead){
        return addHapticObject(pos.x, pos.y, pos.z, frame, number, false);
    }
            
    public MeshEntity addHapticObject(float x, float y, float z, int frame, int number, boolean isBead){
        MeshEntity me = createControlPoint(x, y, z, frame, number, false);
        mf.scene.getEntities().add( me );
        if(isBead){
            final ArrayList<MeshEntity> controlPoints = mf.animPanel.currentAnimation.getControlPoints();
            if (number == -1){
                me.setNumber( controlPoints.size() );
            }
            controlPoints.add( me );
        }else{
            mf.simulation.getControlPoints().add(me);
        }
        return me;
    }
    
    
    private void cpEnableCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpEnableCheckActionPerformed
        Scene.setVisible( mf.scene.getEntities(), Entity.TAG_OBJ, cpEnableCheck.isSelected());
        mf.needUpdate();
    }//GEN-LAST:event_cpEnableCheckActionPerformed

    private void cpSizeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpSizeTextActionPerformed
        final float size = getControlPointSize();
        for (Entity e : mf.selection){
            if (e.getTag() == Entity.TAG_OBJ ){
                e.getTransform().getScale().set( size );
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_cpSizeTextActionPerformed

    private void cpColorTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpColorTextActionPerformed
        final int color = Color.parse( getControlPointColor() );
        for (Entity e : mf.selection){
            if (e.getTag() == Entity.TAG_OBJ && (e instanceof MeshEntity)){
                MeshEntity me = (MeshEntity)e;
                me.setColor( color );
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_cpColorTextActionPerformed

    private void cpAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpAddButtonActionPerformed
        Vector3f sCenter = mf.simulation.getSimulationCenter();
        addHapticObject (sCenter.x, sCenter.y, sCenter.z, getNumber(), 0, false);
        mf.needUpdate();
    }//GEN-LAST:event_cpAddButtonActionPerformed

    public boolean isClickAndPlace(){
        return placeBeadAtSliceCheck.isSelected();
    }
    
    private void cpDelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpDelButtonActionPerformed
        //Remove from simulation and scene
        for (Entity e : mf.selection){
            if (e.getTag() == Entity.TAG_OBJ){
                mf.scene.getEntities().remove( e );
                mf.simulation.controlPoints.remove( e );
            }
        }
        mf.clearSelection();
        mf.needUpdate();
    }//GEN-LAST:event_cpDelButtonActionPerformed

    private void allVisibleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allVisibleButtonActionPerformed
        Scene.setVisible( mf.scene.getEntities() , Entity.TAG_OBJ, true);
    }//GEN-LAST:event_allVisibleButtonActionPerformed

    private void onlyVisibleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onlyVisibleButtonActionPerformed
        setOnlyCurrentCPsVisible( getNumber() , -1);
        mf.needUpdate();
    }//GEN-LAST:event_onlyVisibleButtonActionPerformed

    private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
        selectAll();
        mf.needUpdate();
    }//GEN-LAST:event_selectAllButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        final List<Entity> sel = mf.selection;
        for(Entity e : sel){
            if ( e instanceof ControlPoint){
                ControlPoint s = (ControlPoint)e;
                s.removeAutoTrap();
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List<Transducer> trans ;
        
        if (mf.bag.isEmpty()){
            trans = mf.simulation.transducers;
        }else{
            trans = new ArrayList<>();
            for(Entity e : mf.bag){
                if (e instanceof Transducer){
                    trans.add( (Transducer) e);
                }
            }
        }
        
        
        
        final List<Entity> sel = mf.selection;
        
        for(Entity e : sel){
            if ( e instanceof ControlPoint){
                ControlPoint s = (ControlPoint)e;
                s.setTrap( mf.trapsPanel.getTrap() );
                s.setTrans( trans );
                s.setAutoTrap(true);
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_jButton1ActionPerformed


    
    public void selectAll() {
        mf.clearSelection();
        for(MeshEntity me : mf.scene.getEntities()){
            if ( (me.getTag() & Entity.TAG_OBJ) != 0){
                me.selected = true;
                mf.selection.add(me);
            }
        }
    }

    
    public Entity selectFrame(int frame) {
        mf.clearSelection();
        for(MeshEntity me : mf.scene.getEntities()){
            if ( (me.getTag() & Entity.TAG_OBJ) != 0 &&
                    me.getFrame() == frame){
                me.selected = true;
                mf.selection.add(me);
            }
        }
        if (mf.selection.isEmpty()){
            return null;
        }else{
            return mf.selection.get(0);
        }
    }
    
    public void setOnlyCurrentCPsVisible(int frame, int number){
        Scene.setVisible( mf.scene.getEntities() , Entity.TAG_OBJ, frame, number ,true);
    }
    
    public String getControlPointColor(){
        return cpColorText.getText();
    }
    
    public int getNumber(){
        return Parse.toInt( numberText.getText() );
    }
    
    public float getControlPointSize(){
        return Parse.toFloat( cpSizeText.getText() );
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton allVisibleButton;
    private javax.swing.JButton cpAddButton;
    private javax.swing.JTextField cpColorText;
    private javax.swing.JButton cpDelButton;
    private javax.swing.JCheckBox cpEnableCheck;
    private javax.swing.JTextField cpSizeText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JLabel jLabel43;
    private javax.swing.JTextField numberText;
    private javax.swing.JButton onlyVisibleButton;
    private javax.swing.JCheckBox placeBeadAtSliceCheck;
    private javax.swing.JButton selectAllButton;
    // End of variables declaration//GEN-END:variables



    public void addSelAsBead() {
        //get selected control point
        final Entity selCP = mf.movePanel.getBeadEntity();
        if (selCP == null) { return; }
        
        //add bead clone to the current animation
        final Vector3f pos = selCP.getTransform().getTranslation();
        addHapticObject(pos.x, pos.y, pos.z, 0, -1, true);
    }

    

    
}
